﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BigEaterCh.Kontrolki;

namespace BigEaterCh.Formularze
{
  public partial class FormEditRozchody : Form
  {
    private DateTime _data;

    public FormEditRozchody( DateTime data)
    {
      _data = data;
      InitializeComponent();
      RefreshData();
    }

    void RefreshData()
    {
      FormBigEaterCh.GetDataBase();
      if( FormBigEaterCh.Ze != null )
      {
        var query2 = (from c in FormBigEaterCh.Ze.Przychody
                     join o in FormBigEaterCh.Ze.Produkty on c.IdProduktu equals o.IdProduktu
                    select new
                    {
                      c.IdProduktu,
                      o.Nazwa,
                      o.Jednostka,
                      o.GrupyProduktow,
                      c.CenaBrutto
                    }).Distinct().GroupBy(p => p.GrupyProduktow);


        var query = from c in FormBigEaterCh.Ze.DniProdukty
                     where (c.Data < _data)
                     select c.Data;

        DateTime StartData = DateTime.MinValue;
        DateTime StopData = _data;

        if(query.Count()>0)
        {
          StartData = query.Max();
        }

        if( query2.Count()>0 )
        {
          foreach( var employee in query2 )
          {
            var lvg = new ListViewGroup(employee.Key);
            listView1.Groups.Add(lvg);

            foreach( var pozycje in employee )
            {
              decimal pi = 0;
              decimal ri = 0;
              decimal si = 0;
              var p1 = from c in FormBigEaterCh.Ze.Przychody
                       where
                         (c.IdProduktu == pozycje.IdProduktu && c.Data > StartData && c.Data <= StopData &&
                          c.CenaBrutto == pozycje.CenaBrutto)
                       select c.Ilosc;

              if (p1.Count() > 0)
              {
                pi = p1.Sum();
              }

              var r1 = from c in FormBigEaterCh.Ze.Rozchody
                       where
                         (c.IdProduktu == pozycje.IdProduktu && c.Data > StartData && c.Data <= StopData &&
                          c.CenaBrutto == pozycje.CenaBrutto)
                       select c.Ilosc;

              if (r1.Count() > 0)
              {
                ri = r1.Sum();
              }

              var s1 = from c in FormBigEaterCh.Ze.DniProdukty
                       where
                         (c.IdProduktu == pozycje.IdProduktu && c.Data == StartData &&
                          c.CenaBrutto == pozycje.CenaBrutto)
                       select c.Ilosc;

              if (s1.Count() > 0)
              {
                si = s1.Sum();
              }

              decimal suma = si + pi - ri;

              var lvi = new ListViewItem(pozycje.Nazwa + " - " + pozycje.Jednostka);
              lvi.SubItems.Add(pozycje.CenaBrutto.ToString("C2")).Tag = pozycje.CenaBrutto;
              lvi.SubItems.Add(suma.ToString());
              lvi.Tag = pozycje.IdProduktu;
              lvi.Group = lvg;
              if( suma<0 )
              {
                lvi.BackColor = Color.MistyRose;
              }
              listView1.Items.Add(lvi);
            }
          }
        }
      }
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      foreach(ListViewItem si in listView1.SelectedItems)
      {
        decimal cena = (decimal) si.SubItems[1].Tag;
        int IdProduktu = (int) si.Tag;

        var query1 = from c in FormBigEaterCh.Ze.Rozchody
                     where
                       (c.Data == _data && c.IdProduktu == IdProduktu && c.CenaBrutto == cena)
                     select c;
        if(query1.Count()>0)
        {
          MessageBox.Show(string.Format("Pozycja {0} już jest na liście!", si.Text));
        }
        else
        {
          var dp = FormBigEaterCh.Ze.Rozchody.CreateObject();
          dp.Data = _data;
          dp.IdProduktu = IdProduktu;
          dp.CenaBrutto = cena;
          dp.Ilosc = 0;
          FormBigEaterCh.Ze.AddToRozchody(dp);
        }
      }

      FormBigEaterCh.SaveDataBase();
      FormBigEaterCh.Ze.Refresh(System.Data.Objects.RefreshMode.StoreWins, FormBigEaterCh.Ze.DniPotrawy);

      Close();
    }

    private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {

    }




  }
}
